/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.model;

import java.sql.Date;

import java.util.ArrayList;
import java.util.List;


/**
 * A survey form definition. Persistent class, read-write.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 22-mar-2009 11:04:04
 */
public class Survey implements java.io.Serializable {
	/**
	 * The identifier.
	 */
	private int id;

	/**
	 * The code, must be unique.
	 */
	private String code;

	/**
	 * The title.
	 */
	private String title;

	/**
	 * The description.
	 */
	private String description;

	/**
	 * The creation date.
	 */
	private Date creationDate;

	/**
	 * The start date.
	 */
	private Date startDate;

	/**
	 * The end date.
	 */
	private Date endDate;

	/**
	 * The archived date.
	 */
	private Date archivedDate;

	/**
	 * The lifecycle phase.
	 */
	private SurveyLifecycle lifecycle;

	/**
	 * The organization.
	 */
	private Organization organization;

	/**
	 * The questions collection.
	 */
	private List<Question> questions;

/**
   * Constructor.
   */
	public Survey() {
		questions = new ArrayList<Question>();
		creationDate = new Date(System.currentTimeMillis());
		lifecycle = SurveyLifecycle.DESIGN;
	}

	/**
	 * Retrieves the identifier.
	 *
	 * @return the identifier.
	 */
	public int getId() {
		return this.id;
	}

	/**
	 * Sets the identifier.
	 *
	 * @param id the identifier.
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Retrieves the code.
	 *
	 * @return the code.
	 */
	public String getCode() {
		return this.code;
	}

	/**
	 * Sets the code.
	 *
	 * @param code the code.
	 */
	public void setCode(final String code) {
		this.code = code;
	}

	/**
	 * Retrieves the title.
	 *
	 * @return the title.
	 */
	public String getTitle() {
		return this.title;
	}

	/**
	 * Sets the title.
	 *
	 * @param title the title.
	 */
	public void setTitle(final String title) {
		this.title = title;
	}

	/**
	 * Retrieves the description.
	 *
	 * @return the description.
	 */
	public String getDescription() {
		return this.description;
	}

	/**
	 * Sets the description.
	 *
	 * @param description the description.
	 */
	public void setDescription(final String description) {
		this.description = description;
	}

	/**
	 * Retrieves the creation date.
	 *
	 * @return the creation date.
	 */
	public Date getCreationDate() {
		return this.creationDate;
	}

	/**
	 * Sets the creation date.
	 *
	 * @param creationDate the creation date.
	 */
	public void setCreationDate(final Date creationDate) {
		this.creationDate = creationDate;
	}

	/**
	 * Retrieves the start date.
	 *
	 * @return the start date.
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * Sets the start date.
	 *
	 * @param startDate the start date.
	 */
	public void setStartDate(final Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * Retrieves the end date.
	 *
	 * @return the end date.
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * Sets the end date.
	 *
	 * @param endDate the end date.
	 */
	public void setEndDate(final Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * Retrieves the archive date.
	 *
	 * @return the archive date.
	 */
	public Date getArchivedDate() {
		return archivedDate;
	}

	/**
	 * Sets the archive date.
	 *
	 * @param archivedDate the archive date.
	 */
	public void setArchivedDate(final Date archivedDate) {
		this.archivedDate = archivedDate;
	}

	/**
	 * Retrieves the lifecycle.
	 *
	 * @return the lifecycle.
	 */
	public SurveyLifecycle getLifecycle() {
		return this.lifecycle;
	}

	/**
	 * Sets the lifecycle.
	 *
	 * @param lifecycle the lifecycle.
	 */
	public void setLifecycle(final SurveyLifecycle lifecycle) {
		this.lifecycle = lifecycle;
	}

	/**
	 * Retrieves the organization.
	 *
	 * @return the organization.
	 */
	public Organization getOrganization() {
		return this.organization;
	}

	/**
	 * Retrieves the organization.
	 *
	 * @param organization the organization.
	 */
	public void setOrganization(final Organization organization) {
		this.organization = organization;
	}

	/**
	 * Retrieves the questions.
	 *
	 * @return the questions.
	 */
	public List<Question> getQuestions() {
		return this.questions;
	}

	/**
	 * Sets the questions.
	 *
	 * @param questions the questions.
	 */
	public void setQuestions(final List<Question> questions) {
		this.questions = questions;
	}

	/**
	 * Adds a question.
	 *
	 * @param question a question.
	 */
	public void add(final Question question) {
		question.setSurvey(this);
		getQuestions().add(question);
	}

	/**
	 * Removes a question.
	 *
	 * @param question a question.
	 */
	public void remove(final Question question) {
		getQuestions().remove(question);
		question.setSurvey(null);
	}

	/**
	 * Checks if objects are equal.
	 *
	 * @param o other object.
	 *
	 * @return true if equal, false otherwise.
	 */
	@Override
	public boolean equals(Object o) {
		if(this == o) {
			return true;
		}

		if(!(o instanceof Survey)) {
			return false;
		}

		Survey that = (Survey)o;

		if(getId() != that.getId()) {
			return false;
		}

		return true;
	}

	/**
	 * Calculates the hash of this instance.
	 *
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		return getId();
	}

	/**
	 * Stringifies this instance.
	 *
	 * @return the string representation.
	 */
	public String toString() {
		final StringBuilder sb = new StringBuilder();

		sb.append("Survey");
		sb.append("{id=").append(getId());
		sb.append(", code='").append(getCode()).append('\'');
		sb.append(", title='").append(getTitle()).append('\'');
		sb.append('}');

		return sb.toString();
	}
}
